package com.wyz.note.project.security.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wyz.note.project.security.mapper.RoleMapper;
import com.wyz.note.project.security.mapper.UserMapper;
import com.wyz.note.project.security.pojo.Role;
import com.wyz.note.project.security.pojo.User;
import com.wyz.note.project.security.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @Author: wei yz
 * @Date: 2022/6/18 14:43
 */
@Service
public class UserServiceImpl implements UserService {
    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        log.info("登录的用户名：{}", s);
        User user = userMapper.loadUserByUsername(s);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("用户信息不存在：[" + s + "]");
        }
        log.info("==> 登录成功 <==", s);
        user.setRoles(roleMapper.getUserRoleByUserId(user.getId()));
        log.info("查询角色信息: {}", user.getRoles());
        return user;
    }
}
